<?php
/*
7.2.4 Tasks
Metódy:
•	CreateTask(polia úlohy) – pridá do tabuľky záznam s daným taskom
•	ListAll() – vráti všetky záznamy
•	ListAllFor($id_book) – vráti všetky záznami pre knihu s id $id_book
•	GetById($id) – vráti záznam úlohy so zadaným $id
•	DeleteTask($id) – zmaže záznam s $id
•	Edit($id, polia úlohy) – pre úlohu s $id nastaví polia
*/

class Tasks_model extends CI_Model{

    public function __construct(){
        $this->load->database();
    }

    public function createTask($name,$id_books,$x_units,$y_units,$description,$page,$functions_count){
        $this->db->insert('tasks',array(
            'name' => $name,
            'fk_books' => $id_books,
            'x_units' => $x_units,
            'y_units' => $y_units,
            'description' => $description,
            'page' => $page,
            'functions_count' => $functions_count));
    }

    public function listAll(){
        //return $this->db->get('tasks');
        
        return $this->db->query('SELECT t.*, b.name as book_name FROM tasks t 
            JOIN books b ON b.id=t.fk_books');
    }

    public function listAllFor($id_book){
        return $this->db->get_where('tasks',array('fk_books'=>$id_book));
    }

    public function getById($id){
        $res = $this->db->get_where('tasks',array('id'=>$id))->result();
        return $res[0];
    }
    
     public function getByIdJoined($id){
         
         $res = $this->db->query('SELECT t.*, b.name as book_name FROM tasks t 
            JOIN books b ON b.id=t.fk_books WHERE t.id='.$id)->result();
        return $res[0];
    }

    public function deleteTask($id){
        $this->db->where(array('id'=>$id));
        $this->db->delete('tasks');
    }

    public function edit($id, $name, $id_books, $x_units, $y_units, $description, $page, $functions_count){
        $this->db->where('id', $id);
        $this->db->update('tasks',array(
            'name' => $name,
            'fk_books' => $id_books,
            'x_units' => $x_units,
            'y_units' => $y_units,
            'description' => $description,
            'page' => $page,
            'functions_count' => $functions_count));
    }



}

?>